package com.nannybear.Controller;

import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.nannybear.R;
import com.nannybear.View.RangeSeekBar;

/**
 * @brief this class add all the widget and layout to the settings screen and add listener
 */
public class ParametersController {
    /********** DECLARATIONS **********/
    // Variables
    private int soundValue;
    private int lightValue_dark_dim = 0;
    private int lightValue_dim_bright = 0;

    // Classes
	private ViewPuppeteer viewPuppeteer;


    /************ METHODS ************/
    /**
     * @brief constructor
     * @param viewPuppeteer : takes the class ViewPuppeeter in parameter
     */
	public ParametersController (ViewPuppeteer viewPuppeteer) {
		this.viewPuppeteer = viewPuppeteer;
        this.initInterface();
        this.addSpyButtons();
        this.displayCurrentView();
        soundValue = getViewPuppeteer().getmActivity().getSoundThreshold().getValueSound();

	}

    /**
     * @brief Instantiation of the widget and layout of the view.
     */
    private void initInterface(){
        // Choice buttons
        this.viewPuppeteer.getmActivity().getSettingsScreen().setChoice_name((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.choice_name));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setChoice_parameter((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.choice_parameters));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setChoice_connection((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.choice_connection));

        // Widget of the view
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTxt_connection((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.txt_connection));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setConnection_btn((Button) this.viewPuppeteer.getmActivity().findViewById(R.id.connection_button));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setWifi_state((ImageView) this.viewPuppeteer.getmActivity().findViewById(R.id.wifi_state));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTxt_change_threshold((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.change_threshold));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTxt_change_sound((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.change_sound));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTxt_change_light((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.change_light));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_quiet_end((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_quiet_end));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_noise_start((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_noise_start));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_dark_end((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_dark_end));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_dim_start((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_dim_start));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_dim_end((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_dim_end));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setValue_bright_start((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.value_light_start));

        // Layout of the view
        this.viewPuppeteer.getmActivity().getSettingsScreen().setBb_name_layout((LinearLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.bb_name_layout));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setChoice_sound_layout((LinearLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.change_sound_layout));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setChoice_connection_layout((LinearLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.choice_connection_layout));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setChoice_light_layout((LinearLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.change_light_layout));
        this.viewPuppeteer.getmActivity().getSettingsScreen().setTxt_threshold_light((RelativeLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.threshold_light_layout));
    }

    /**
     * @brief Instantiation of the button or seekBar to listen
     */
	private void addSpyButtons(){
        ImageButton choice_name_btn = (ImageButton)this.viewPuppeteer.getmActivity().findViewById(R.id.button_name);
        ImageButton choice_parameters_btn = (ImageButton)this.viewPuppeteer.getmActivity().findViewById(R.id.button_parameter);
        ImageButton choice_connection_btn = (ImageButton)this.viewPuppeteer.getmActivity().findViewById(R.id.button_connection);
        ImageButton choice_sound_btn = (ImageButton)this.viewPuppeteer.getmActivity().findViewById(R.id.button_sound);
        ImageButton choice_light_btn = (ImageButton)this.viewPuppeteer.getmActivity().findViewById(R.id.button_light);
        LinearLayout lightSeekBar_layout = (LinearLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.light_seekbar);
        SeekBar sound_seekbar = (SeekBar) this.viewPuppeteer.getmActivity().findViewById(R.id.sound_seekBar);


        // create RangeSeekBar as Integer range between 0 and 100
        RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(0, 100, this.viewPuppeteer.getmActivity().getBaseContext());
        //Set the button to initialized place

        double MinValue = ( this.getViewPuppeteer().getmActivity().getLightThreshold().getDarkDimThreshold() ) / 100d;
        seekBar.setNormalizedMinValue(MinValue);
        double MaxValue = ( this.getViewPuppeteer().getmActivity().getLightThreshold().getDimBrightThreshold() ) / 100d;
        seekBar.setNormalizedMaxValue(MaxValue);

        //listening settings buttons
        choice_name_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	getViewPuppeteer().getmActivity().getSettingsScreen().displayNameButtonClick();
            }
        });    

        choice_parameters_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	getViewPuppeteer().getmActivity().getSettingsScreen().displayParameterButtonClick();
            }
        });

        choice_sound_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getViewPuppeteer().getmActivity().getSettingsScreen().displaySoundButtonClick();
            }
        });

        choice_light_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getViewPuppeteer().getmActivity().getSettingsScreen().displayLightButtonClick();
            }
        });

        choice_connection_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	getViewPuppeteer().getmActivity().getSettingsScreen().displayConnectionButtonClick();
             }
         });

        //listening settings seekBar
        seekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
            @Override
            public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer valueDarkDim, Integer valueDimBright, boolean sendData) {
                int lightThreshold_dark_dim = valueDarkDim;
                int lightThreshold_dim_bright = valueDimBright;
                setLightValue_dark_dim(lightThreshold_dark_dim);
                setLightValue_dim_bright(lightThreshold_dim_bright);
                getViewPuppeteer().getmActivity().getSettingsScreen().displayValueLight(lightThreshold_dark_dim, lightThreshold_dim_bright);
                if(sendData == true){
                    sendNewLightThreshold();
                }
            }
        });

        sound_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            boolean cursor_pressed = false;
            int soundThreshold = 0;
            @Override
            public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
                this.soundThreshold = progressValue;
                if(cursor_pressed==true && fromUser==true){
                    setSoundValue(soundThreshold);
                    getViewPuppeteer().getmActivity().getSettingsScreen().displayValueSound(soundThreshold);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                cursor_pressed=true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                cursor_pressed=false;
                sendNewSoundThreshold();
            }
            });


        // Instantiation of the button listened
        this.viewPuppeteer.getmActivity().getSettingsScreen().setChoice_name_btn(choice_name_btn);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setChoice_connection_btn(choice_connection_btn);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setChoice_parameters_btn(choice_parameters_btn);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setChoice_sound_btn(choice_sound_btn);
        this.viewPuppeteer.getmActivity().getSettingsScreen().setChoice_light_btn(choice_light_btn);

        //add seekBar in the good layout
        this.viewPuppeteer.getmActivity().getSettingsScreen().setLight_seekbar(lightSeekBar_layout);
        this.viewPuppeteer.getmActivity().getSettingsScreen().getLight_seekbar().addView(seekBar);
	}

    /**
     * @brief Send new sound threshold.
     */
    public void sendNewSoundThreshold() {
        getViewPuppeteer().getmActivity().getSoundThreshold().setValueSound(getSoundValue());
    }

    /**
     * @brief Send new light threshold (between dark and dim).
     */
    public void sendNewLightThreshold() {
        String lightThresholds;
        lightThresholds = getLightValue_dark_dim()+"/"+getLightValue_dim_bright();
        getViewPuppeteer().getmActivity().getLightThreshold().setLightThresholds(lightThresholds);
    }

    /**
     * @brief Call the methods to display the view
     */
	public void displayCurrentView(){
		this.viewPuppeteer.displayCurrentView();
	}

    /*********** ACCESSORS ***********/
	public ViewPuppeteer getViewPuppeteer() {
		return viewPuppeteer;
	}

	public void setViewPuppeteer(ViewPuppeteer viewPuppeteer) {
		this.viewPuppeteer = viewPuppeteer;
	}

    public int getSoundValue() {
        return soundValue;
    }

    public void setSoundValue(int soundValue) {
        this.soundValue = soundValue;
    }

    public int getLightValue_dark_dim() {
        return lightValue_dark_dim;
    }

    public void setLightValue_dark_dim(int lightValue_dark_dim) {
        this.lightValue_dark_dim = lightValue_dark_dim;
    }

    public int getLightValue_dim_bright() {
        return lightValue_dim_bright;
    }

    public void setLightValue_dim_bright(int lightValue_dim_bright) {
        this.lightValue_dim_bright = lightValue_dim_bright;
    }
}
